﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppEquipmentManage;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EquipmentController : ControllerBase
    {
        private readonly ILogUtil _logger;
        private readonly EquipmentApp _equipmentApp;
        public EquipmentController(ILogUtil logger, EquipmentApp equipmentApp)
        {
            _logger = logger;
            _equipmentApp = equipmentApp;
        }

        #region 调度设备
        [HttpPost]
        public async Task<HeaderResult<long>> AddDispatchEquipmentAsync(DispatchEquipmentInfoDto DispatchEquipmentInfoDto)
        {
            try
            {
                long result = await _equipmentApp.AddDispatchEquipmentAsync(DispatchEquipmentInfoDto);
                return ApiBase.Success(result, "新增调度设备成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<long>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<bool>> DeleteDispatchEquipmentAsync(long ID)
        {
            try
            {
                bool result = await _equipmentApp.DeleteDispatchEquipmentAsync(ID);
                return ApiBase.Success<bool>("删除调度设备成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> UpdateDispatchEquipmentAsync(DispatchEquipmentInfoDto DispatchEquipmentInfoDto)
        {
            try
            {
                bool result = await _equipmentApp.UpdateDispatchEquipmentAsync(DispatchEquipmentInfoDto);
                return ApiBase.Success<bool>("修改调度设备成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<List<DispatchEquipmentInfoDto>>> GetAllDispatchEquipmentAsync(QueryDispatchEquipmentInfoDto queryDispatchEquipmentDto)
        {
            try
            {
                var pageList = await _equipmentApp.GetDispatchEquipmentAsync(queryDispatchEquipmentDto);
                return ApiBase.Info(pageList.Result, pageList.Total, "查询成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<List<DispatchEquipmentInfoDto>>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<DispatchEquipmentInfoDto>> GetDispatchEquipmentByIDAsync(long ID)
        {
            try
            {
                var result = await _equipmentApp.GetDispatchEquipmentByIDAsync(ID);
                return ApiBase.Success(result,"获取数据成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<DispatchEquipmentInfoDto>(ex.Message);
            }
        }
        #endregion

        #region 设备类型
        [HttpPost]
        public async Task<HeaderResult<long>> AddEquipmentTypeAsync(EquipmentTypeInfoDto EquipmentTypeInfoDto)
        {
            try
            {
                long result = await _equipmentApp.AddEquipmentTypeAsync(EquipmentTypeInfoDto);
                return ApiBase.Success(result, "新增设备类型成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<long>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<bool>> DeleteEquipmentTypeAsync(long ID)
        {
            try
            {
                bool result = await _equipmentApp.DeleteEquipmentTypeAsync(ID);
                return ApiBase.Success<bool>("删除设备类型成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> UpdateEquipmentTypeAsync(EquipmentTypeInfoDto EquipmentTypeInfoDto)
        {
            try
            {
                bool result = await _equipmentApp.UpdateEquipmentTypeAsync(EquipmentTypeInfoDto);
                return ApiBase.Success<bool>("修改设备类型成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<bool>(ex.Message);
            }
        }

        [HttpPost]
        public async Task<HeaderResult<List<EquipmentTypeInfoDto>>> GetAllEquipmentTypeAsync(QueryEquipmentTypeInfoDto queryEquipmentTypeDto)
        {
            try
            {
                var list = await _equipmentApp.GetEquipmentTypeAsync(queryEquipmentTypeDto);
                return ApiBase.Success(list, "查询成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<List<EquipmentTypeInfoDto>>(ex.Message);
            }
        }

        [HttpGet]
        public async Task<HeaderResult<EquipmentTypeInfoDto>> GetEquipmentTypeByIDAsync(long ID)
        {
            try
            {
                var result = await _equipmentApp.GetEquipmentTypeByIDAsync(ID);
                return ApiBase.Success(result, "获取数据成功");
            }
            catch (Exception ex)
            {
                _logger.Error(ex.ToString());
                return ApiBase.Error<EquipmentTypeInfoDto>(ex.Message);
            }
        }
        #endregion
    }
}
